package com.longyiqun.android.zhihu.Activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.LayoutRes;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

import com.longyiqun.android.zhihu.R;

// Base类, 每个Activity都要继承这个类
public class BaseActivity extends AppCompatActivity {

    // 载入Tool Bar
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        Toolbar myToolBar = (Toolbar) findViewById(R.id.my_toolbar);
        setSupportActionBar(myToolBar);
    }

    // Option选项
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    // 重写了setContentView方法
    public void setContentView2(@LayoutRes int layoutResID) {
        LinearLayout layout = (LinearLayout) findViewById(R.id.maincontent);
        View view = LayoutInflater.from(this).inflate(layoutResID, null);
        layout.addView(view);
    }

    // 隐藏小工具 --- 21Cake API
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.cakeGo:
                dialog();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    // 弹出Dialog方法
    protected void dialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(BaseActivity.this);
        builder.setMessage("轰炸21Cake?");
        builder.setTitle("Warning");
        builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                goTestCake();
            }
        });
        builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        builder.create().show();
    }

    protected void goTestCake() {
        Intent intent = new Intent(this, CakeActivity.class);
        // 开启意图
        startActivity(intent);
    }
}
